# from google.appengine.api import app_identity	 import app_identity	
import json
import urllib2

# these are the environmental variables, you can change the mapid and the apikey to
# thier values that make sense for your map viewer application.
# the apikey is the api key of your app engine app, the mapid is the mapid of the GME
# map that you want to display in your custom mapviewer application. The layers of the map
# will be populated in the layer selector with on/off tickboxes. You will also need to
# add the apps identity to your maps access list.

# the appIdentity is for this app is: templatemapviewer@appspot.gserviceaccount.com
# this is the name that you would add to your GME map editors access list so that the
# app can gain access to your maps information (ie layer id/keys that are included in the map).

# you can get the exact appidentity by calling this function "google.appengine.api.app_identity.get_application_id()

apiKey = "AIzaSyBX10jg_4M3w9J5KRGmrgqcObAs7Me4KfM"
mapid = "16075412419356476951-03664761241047799960"
scopes = ['https://www.googleapis.com/auth/mapsengine.readonly']

# this function is used to get a token for the application in order to allow the application to
# see the maps layer information.
def getToken():
	# This try statement only works because the actual templatemapviewer.appspot.com app exists and is serving out
	# the application token in an insecure way. This is done becauase google.appengine.api import app_identity	
	# cannot be imported in the dev environment.
	try:
		token, x = app_identity.get_access_token(scopes)
	except:
		token = urllib2.urlopen("http://templatemapviewer.appspot.com/templatemapviewer/default/token.html").read()

	return token


# this function is just here to serve the token.html page which
# is used to get a temporary token for the dev environment.
def token():

	token, x = app_identity.get_access_token(scopes)

	return dict(token=token)


def index():

	token = getToken()
	# this url will return the layer information for a given map. The map is identified via the mapid variable.
	# for this URL to return something meaningful, you will need to add the apps identity to the required maps
	# access list.
	url = "https://www.googleapis.com/mapsengine/v1/maps/%s?access_token=%s&key=%s" % (mapid, token, apiKey)
	response = urllib2.urlopen(url)
	data = json.loads(response.read())

	mapName = data["name"]

	# The below constructs some data from the json data returned by the above url request
	# the values that are needed are taken from the json data and used to create a more relevant
	# set of data that will be used by the view.
	mapData = []

	for layer in data["contents"]:
		name = layer["name"].replace(" ","").replace("-","")
		displayname = layer["name"]
		key = layer["key"]

		layerInfo = (name, displayname, key)
		mapData.append(layerInfo)

	return dict(apiKey=apiKey, mapData=mapData, mapid=mapid, mapName=mapName)


# def sandbox():
# 	token = getToken()

# 	url = "https://www.googleapis.com/mapsengine/v1/layers/16075412419356476951-10162219797617067105?access_token=%s&key=%s" % (token, apiKey)
# 	response = urllib2.urlopen(url)
# 	data = json.loads(response.read())

# 	return dict(data=data)